package com.example.m3u8;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    //PlayerView 视频播放组件
    PlayerView playerView;
    //SimpleExoPlayer是ExoPlayer接口的一个默认的通用实现
    SimpleExoPlayer exoPlayer;
    //生成数据媒体实例，通过该实例加载媒体数据
    DataSource.Factory mediaDataSourceFactory;
    // 得到默认合适的带宽
    private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
    private MediaSource dashMediaSource; //如果播放流媒体则使用MediaSource 如果使用m3u8 HLS 则使用HLsMediaSource
    /// 创建跟踪器
    private DefaultTrackSelector trackSelector;
    private HlsMediaSource hlsMediaSource;// 如果使用m3u8 HLS 则使用HLsMediaSource 如果播放流媒体则使用MediaSource

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView = findViewById(R.id.exoplayer_view);

        mediaDataSourceFactory = buildDataSourceFactory(true);
        //mediaDataSourceFactory=new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayerdemo"));
        initMediaPlayer();
    }

    private void initMediaPlayer() {
        Uri uri = Uri.parse("http://121.31.30.90:8085/ysten-business/live/cctv-6/1.m3u8");
        TrackSelection.Factory trackFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);

        DefaultTrackSelector.Parameters trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
        trackSelector = new DefaultTrackSelector(trackFactory);
        trackSelector.setParameters(trackSelectorParameters);

        exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
        playerView.setPlayer(exoPlayer);
        //设置缓存数据
        hlsMediaSource = new HlsMediaSource(uri, new CacheDataSourceFactory(this,100 * 1024 * 1024, 5 * 1024 * 1024), null, null);
           // hlsMediaSource=new HlsMediaSource(uri,mediaDataSourceFactory,null,null);
        exoPlayer.prepare(hlsMediaSource);
        exoPlayer.setPlayWhenReady(true);
    }

    //condition ? exprIfTrue : exprIfFalse .
    // condition 计算结果用作条件的表达式 如果表达式condition 为true 则 exprIfTrue 否则则为 ExprIFalse;
    private DataSource.Factory buildDataSourceFactory(boolean useBandwithMeter) {

        return ((MyApplication) getApplication()).buildDataSourceFactory(useBandwithMeter ? null : null);
    }

    @Override
    protected void onStop() {
        super.onStop();
        releasePlayer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releasePlayer();
    }

    private void releasePlayer() {
        if (exoPlayer != null) {
            exoPlayer.release();
            exoPlayer = null;
            hlsMediaSource = null;
            trackSelector = null;
        }
    }
}